/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月29日         luwei
 */

package com.coocaa.launcher.framework.sidebar.item;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;

import com.coocaa.launcher.framework.sidebar.ItemContainer.IItem;
import com.coocaa.launcher.framework.sidebar.ItemContainer.OnExtraViewListener;
import com.coocaa.launcher.framework.sidebar.OnEventListener;
import com.coocaa.launcher.framework.sidebar.item.ItemFactory.ITEM;
import com.coocaa.launcher.framework.sidebar.item.ItemView.OnItemClickListener;
import com.coocaa.launcher.framework.sidebar.item.ItemView.OnItemFocusChangeListener;
import com.coocaa.launcher.framework.sidebar.item.ItemView.OnItemKeyEventListener;
import com.coocaa.launcher.sidebar.setting.layout.wallpaper.OnKeyTurnPageListener;
import com.tianci.appstore.utils.SkyConstants;

public abstract class Item implements IItem, OnItemFocusChangeListener, OnItemKeyEventListener, OnItemClickListener {
    protected Context mContext = null;
    protected OnExtraViewListener mExtraViewListener = null;
    protected ItemView mItemView = null;
    protected LayoutVisibleCallBack mVisibleCallBack = null;
    protected ITEM mType = null;
    protected OnEventListener mEventListener = null;
    protected int mIndex = 0;
    protected boolean[] mTurnPage = null;
    protected OnKeyTurnPageListener mKeyTurnPageListener = null;

    public Item(Context context) {
        this.mContext = context;
        mItemView = createItemView();
        mItemView.setItemFocusChangeListener(this);
        mItemView.setItemKeyEventListener(this);
        mItemView.setItemClickListener(this);
    }

    @Override
    public void setExtraViewListener(OnExtraViewListener listener) {
        this.mExtraViewListener = listener;
    }

    @Override
    public void setLayoutVisibleCallBack(LayoutVisibleCallBack callBack) {
        this.mVisibleCallBack = callBack;
    }

    @Override
    public void setIndex(int index) {
        this.mIndex = index;
    }

    @Override
    public int getIndex() {
        return mIndex;
    }

    @Override
    public View getView() {
        return mItemView;
    }

    @Override
    public void setEventListener(OnEventListener listener) {
        this.mEventListener = listener;
    }

    @Override
    public void onItemClick(View v) {
        if (mEventListener != null) {
            mEventListener.onClick(this, -1);
        }
    }

    @Override
    public void onItemFocusChange(View v, boolean focus) {
        if (mEventListener != null) {
            mEventListener.onFocusChange(this, focus);
        }
    }

    @Override
    public boolean onItemKeyEvent(View v, int keycode, KeyEvent keyEvent) {
        if (mKeyTurnPageListener != null) {
            return mKeyTurnPageListener.onKeyDown(this, keycode, mIndex, mTurnPage);
        }
        return false;
    }

    @Override
    public void setTurnPage(boolean[] turnPage) {
        // TODO Auto-generated method stub
        this.mTurnPage = turnPage;
    }

    public void setKeyTurnPageListener(OnKeyTurnPageListener listener) {
        this.mKeyTurnPageListener = listener;
    }

    ;

    @Override
    public void setType(ITEM type) {
        this.mType = type;
    }

    @Override
    public ITEM getType() {
        return mType;
    }

    @Override
    public void destory() {
        mItemView.destory();
    }

    @Override
    public void setData(Object object) {

    }

    public static int Div(int x) {
        return SkyConstants.Div(x);
    }

    public static int Dpi(int x) {
        return SkyConstants.Dpi(x);
    }
}
